package com.music.service.music.impl;

import com.music.dto.collection.CollectionSongDTO;
import com.music.dto.music.SongDTO;
import com.music.dto.utils.PagingDTO;
import com.music.dto.utils.TableReqDTO;
import com.music.dto.utils.TableRspDTO;
import com.music.entity.collection.CollectionSongEntity;
import com.music.mapper.music.CollectionSongMapper;
import com.music.service.music.CollectionSongService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CollectionSongServiceImpl implements CollectionSongService {

    @Resource
    private CollectionSongMapper collectionSongMapper;
    private final Logger logger = LoggerFactory.getLogger(SongServiceImpl.class);

    @Override
    public TableRspDTO list4Table(Long userId, TableReqDTO tableReqDTO) throws Exception {
        Long count = collectionSongMapper.count4Table(
                userId,
                tableReqDTO.parseQueryParam("songName"),
                tableReqDTO.parseQueryParam("singerName"),
                tableReqDTO.parseQueryParam("albumName")
        );
        List<SongDTO> listSong = collectionSongMapper.list4Table(
                tableReqDTO.getStart(),
                tableReqDTO.getPageSize(),
                userId,
                tableReqDTO.parseQueryParam("songName"),
                tableReqDTO.parseQueryParam("singerName"),
                tableReqDTO.parseQueryParam("albumName"),
                tableReqDTO.getSortField(),
                tableReqDTO.getSortOrder());
        for (int i = 0; i < listSong.size(); i++)
            listSong.get(i).setOrderNumber(1 + i + tableReqDTO.getStart());
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                listSong);
        return new TableRspDTO(pagingDTO);
    }

    @Override
    public Integer add(Long userId, CollectionSongDTO collectionSongDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        CollectionSongEntity collectionSongEntity = mapperFactory.getMapperFacade()
                .map(collectionSongDTO, CollectionSongEntity.class);
        collectionSongEntity.setGmtCreate(new Date());
        collectionSongEntity.setUserId(userId);
        return collectionSongMapper.addCollectionSong(collectionSongEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String collectionSongIds) throws Exception {
        String[] songId = collectionSongIds.split(",");
        for (String id : songId) {
            collectionSongMapper.remove(Long.parseLong(id));
        }
        return songId.length;
    }

    @Override
    public Integer removeByUserId(Long userId, Long songId) throws Exception {
        return collectionSongMapper.removeByUserId(userId, songId);
    }
}
